package com.aplink.generic.widgets;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aplink.dellcommercial.R;
import com.aplink.generic.util.CameraUtils;
import com.aplink.generic.util.convertcount.GenericConverter;
import com.urlImageCapture.BaseClaimPagerAdapter.OnItemClaimChangeListener;
import com.urlImageCapture.ClaimImage;
import com.urlImageCapture.GalleryClaimViewPager;
import com.urlImageCapture.MyImage;
import com.urlImageCapture.UrlPagerClaimAdapter;

import java.util.ArrayList;
import java.util.List;

public class ImageCaptureFragment extends android.support.v4.app.Fragment {
    public static Bitmap currentBitmap;
    public static Activity mContext;
    public static final int REQUEST_TAKE_PHOTO = 0x01;
    private Button btnImage;
    private int idImagePathControl;
    private TextView imagePathCtrl;
    private ImageView imgNoPhoto;
    private ViewGroup layoutImageCapture;
    private final List<ClaimImage> mClaimImages = new ArrayList<ClaimImage>();

    private GalleryClaimViewPager mViewPager;
    private UrlPagerClaimAdapter pagerAdapter;
    private int positionImage = 0;

    public static ImageCaptureFragment newInstance( Activity parentActivity,
                                                    int idImagePathControl ) {
        final ImageCaptureFragment imageCaptureFragment = new ImageCaptureFragment();
        final Bundle args = new Bundle();
        args.putInt( "ImagePathCtrl", idImagePathControl );
        imageCaptureFragment.setArguments( args );
        ImageCaptureFragment.mContext = parentActivity;

        return imageCaptureFragment;
    }

    private void showImage( List<ClaimImage> items ) {

        if ( items.size() == 0 ) {
            this.mViewPager.setVisibility( View.GONE );
            this.imgNoPhoto.setVisibility( View.VISIBLE );
            return;
        }

        this.mViewPager.setVisibility( View.VISIBLE );
        this.imgNoPhoto.setVisibility( View.GONE );

        this.pagerAdapter = new UrlPagerClaimAdapter( this.getActivity()
                .getBaseContext(), new ArrayList<MyImage>( items ) );
        this.mViewPager.setOffscreenPageLimit( 3 );
        this.mViewPager.setAdapter( this.pagerAdapter );
        this.pagerAdapter
                .setOnItemChangeListener( new OnItemClaimChangeListener() {

                    @Override
                    public void onItemChange( int currentPosition ) {
                        ImageCaptureFragment.this
                                .setPositionImage( currentPosition );
                    }
                } );
    }

    public int getPositionImage() {
        return this.positionImage;
    }

    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data ) {
        super.onActivityResult( requestCode, resultCode, data );
        switch ( requestCode ) {

            case REQUEST_TAKE_PHOTO:
                if ( resultCode == Activity.RESULT_OK ) {
                    this.imgNoPhoto.setVisibility( View.GONE );
                    this.mViewPager.setVisibility( View.VISIBLE );
                    this.showImage( this.mClaimImages );
                }
                break;
        }
    }

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        this.idImagePathControl = this.getArguments() != null ? this
                .getArguments().getInt( "ImagePathCtrl" ) : - 1;
    }

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState ) {

        if ( this.idImagePathControl == - 1 ) {
            // there is no control to store image paths
            // therefore we return null view
            return null;
        }

        this.imagePathCtrl = (TextView) GenericConverter.getView( mContext,
                this.idImagePathControl );

        this.layoutImageCapture = (ViewGroup) inflater.inflate(
                R.layout.aplink__generic_image_capture, null );
        if ( this.mViewPager == null ) {
            this.mViewPager = (GalleryClaimViewPager) this.layoutImageCapture
                    .findViewById( R.id.gallery_detail_claim );
        }

        if ( this.imgNoPhoto == null ) {
            this.imgNoPhoto = (ImageView) this.layoutImageCapture
                    .findViewById( R.id.img_barcode );
        }

        if ( this.btnImage == null ) {
            this.btnImage = (Button) this.layoutImageCapture
                    .findViewById( R.id.btn_take_photo );
        }

        this.btnImage.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick( View v ) {

                // Intent intCreateClaim = CameraUtils
                // .startCameraToTakePhoto(parentActivity);
                final Intent intCreateClaim = CameraUtils
                        .startCameraToTakePhoto( ImageCaptureFragment.mContext
                                .getApplicationContext() );

                if ( intCreateClaim != null ) {

                    final ClaimImage ci = new ClaimImage();

                    ci.setImageLocation( (Uri) intCreateClaim
                            .getParcelableExtra( MediaStore.EXTRA_OUTPUT ) );

                    final String currentPath = ImageCaptureFragment.this.imagePathCtrl
                            .getText().toString();
                    ImageCaptureFragment.this.imagePathCtrl.setText( currentPath
                            + ";" + ci.getImageLocation() );
                    // currentPath = imagePathCtrl.getText().toString();
                    // System.out.println("<<<<<<<" + currentPath);
                    ImageCaptureFragment.this.mClaimImages.add( ci );

                    try {

                        ImageCaptureFragment.this.startActivityForResult(
                                intCreateClaim, REQUEST_TAKE_PHOTO );

                    } catch ( final ActivityNotFoundException ane ) {
                        Log.e( "hc",
                                "ERROR when calling camera app: "
                                        + ane.toString() );
                    }
                }
            }
        } );

        return this.layoutImageCapture;
    }

    @Override
    public void onResume() {
        super.onResume();
        if ( this.mClaimImages.size() != 0 ) {
            this.showImage( this.mClaimImages );
        }
    }

    public void setPositionImage( int positionImage ) {
        this.positionImage = positionImage;
    }

}
